package com.oracle.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.oracle.common.PageResult;
import com.oracle.mapper.FkMapper;
import com.oracle.pojo.Fk;
import com.oracle.pojo.FkExample;
import com.oracle.service.FkService;
import com.oracle.vo.FkVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class FkServiceImpl implements FkService {
    @Autowired(required = false)
    private FkMapper fkMapper;

    @Override
    public List<Fk> findList()
    {
        List<Fk> fks = fkMapper.selectByExample(null);
        return fks;
    }

    @Override
    public PageResult findPages(Integer pageNo, Integer pageSize,Fk fk)
    {
        PageHelper.startPage(pageNo,pageSize);

        FkExample example = new FkExample();
        FkExample.Criteria criteria = example.createCriteria();

        if (!ObjectUtils.isEmpty(fk.getFkId()))
        {
            criteria.andFkIdEqualTo(fk.getFkId());
        }
        if (!ObjectUtils.isEmpty(fk.getFkName()))
        {
            criteria.andFkNameEqualTo(fk.getFkName());
        }
        if (!ObjectUtils.isEmpty(fk.getFkPhone()))
        {
            criteria.andFkPhoneEqualTo(fk.getFkPhone());
        }
        if (!ObjectUtils.isEmpty(fk.getFkDormbuild()))
        {
            criteria.andFkDormbuildEqualTo(fk.getFkDormbuild());
        }
        if (!ObjectUtils.isEmpty(fk.getFkDormitoryid()))
        {
            criteria.andFkDormitoryidEqualTo(fk.getFkDormitoryid());
        }
        if (!ObjectUtils.isEmpty(fk.getFkTime()))
        {
            criteria.andFkTimeEqualTo(fk.getFkTime());
        }

        example.setOrderByClause("fk_id");

        Page<Fk> fkPage = (Page<Fk>) fkMapper.selectByExample(example);

        List<Fk> fkList = fkPage.getResult();

        List<FkVo> fkVoList = bulidFkVoList(fkList);

        return new PageResult(fkPage.getTotal(),fkVoList);
    }

    private List<FkVo> bulidFkVoList(List<Fk> fkList)
    {
        List<FkVo> fkVoList = new ArrayList<>();
        fkVoList.forEach(fk -> {
            FkVo fkVo1 = new FkVo();
            BeanUtils.copyProperties(fk,fkVo1);
            fkVoList.add(fkVo1);
        });
        return fkVoList;
    }
}
